#include <stdio.h>
#include <REGX52.H>
#include "LCD1602.h"
#include <math.h>

int Ke = 1, line = 1;

double KKe = 0, mode = 0, sum = 0, sum1 = 0;

void Delay1ms(int s)	//@11.0592MHz
{
	while (s >= 1) {
		unsigned char data i, j;
		i = 2;
		j = 199;
		do
		{
			while (--j);
		} while (--i);
		s--;
	}
}

unsigned char Key() {
	unsigned char K = 0;
	P1 = 0xFF;
	P1_7 = 0;
	if (P1_3 == 0) { Delay1ms(20); while (P1_3 == 0); Delay1ms(20); K = 1; }
	if (P1_2 == 0) { Delay1ms(20); while (P1_2 == 0); Delay1ms(20); K = 2; }
	if (P1_1 == 0) { Delay1ms(20); while (P1_1 == 0); Delay1ms(20); K = 3; }
	if (P1_0 == 0) { Delay1ms(20); while (P1_0 == 0); Delay1ms(20); K = 4; }

	P1 = 0xFF;
	P1_6 = 0;
	if (P1_3 == 0) { Delay1ms(20); while (P1_3 == 0); Delay1ms(20); K = 5; }
	if (P1_2 == 0) { Delay1ms(20); while (P1_2 == 0); Delay1ms(20); K = 6; }
	if (P1_1 == 0) { Delay1ms(20); while (P1_1 == 0); Delay1ms(20); K = 7; }
	if (P1_0 == 0) { Delay1ms(20); while (P1_0 == 0); Delay1ms(20); K = 8; }

	P1 = 0xFF;
	P1_5 = 0;
	if (P1_3 == 0) { Delay1ms(20); while (P1_3 == 0); Delay1ms(20); K = 9; }
	if (P1_2 == 0) { Delay1ms(20); while (P1_2 == 0); Delay1ms(20); K = 10; }
	if (P1_1 == 0) { Delay1ms(20); while (P1_1 == 0); Delay1ms(20); K = 11; }
	if (P1_0 == 0) { Delay1ms(20); while (P1_0 == 0); Delay1ms(20); K = 12; }

	P1 = 0xFF;
	P1_4 = 0;
	if (P1_3 == 0) { Delay1ms(20); while (P1_3 == 0); Delay1ms(20); K = 13; }
	if (P1_2 == 0) { Delay1ms(20); while (P1_2 == 0); Delay1ms(20); K = 14; }
	if (P1_1 == 0) { Delay1ms(20); while (P1_1 == 0); Delay1ms(20); K = 15; }
	if (P1_0 == 0) { Delay1ms(20); while (P1_0 == 0); Delay1ms(20); K = 16; }

	return K;

}

long num1(long n1, Ke, sum) {
	if (Ke == 1) {
		sum = n1;
	}
	else {
		sum = sum * 10 + n1;
	}
	return sum;
}

long num2(long n1, Ke, sum) {
	if (Ke == KKe) {
		sum = n1;
	}
	else {
		sum = sum * 10 + n1;
	}
	return sum;
}

long back(long sum, Ke) {
	if (Ke == 1)sum = 0;
	else { sum = sum / 10; }
	return sum;
}

int weishu(double num) {
	unsigned char n = 1;

	for (; num >= 10; num /= 10) {
		n++;
	}

	return n;

}
int i = 1;

unsigned char f(int line) {
	if (line == 1)return 2;
	else { return 1; }
}

//---------------------------main---------------------
int main(void) {
	LCD_Init();

	line = 2;

	//------------------------num1-------------------


	while (1) {
		switch (Key()) {
		case 1:LCD_ShowNum(f(line), Ke, 1, 1); sum = num1(1, Ke, sum); Ke++; break;
		case 2:LCD_ShowNum(f(line), Ke, 2, 1); sum = num1(2, Ke, sum); Ke++; break;
		case 3:LCD_ShowNum(f(line), Ke, 3, 1); sum = num1(3, Ke, sum); Ke++; break;

		case 4: {
			if (Ke == 1)break;
			else {
				LCD_ShowChar(f(line), Ke, '+');
				mode = 1;
				Ke++;
				break;
			}
		}

		case 5:LCD_ShowNum(f(line), Ke, 4, 1); sum = num1(4, Ke, sum); Ke++; break;
		case 6:LCD_ShowNum(f(line), Ke, 5, 1); sum = num1(5, Ke, sum); Ke++; break;
		case 7:LCD_ShowNum(f(line), Ke, 6, 1); sum = num1(6, Ke, sum); Ke++; break;

		case 8: {
			if (Ke == 1)break;
			else {
				LCD_ShowChar(f(line), Ke, '-');
				mode = 2;
				Ke++;
				break;
			}
		}

		case 9:LCD_ShowNum(f(line), Ke, 7, 1); sum = num1(7, Ke, sum); Ke++; break;
		case 10:LCD_ShowNum(f(line), Ke, 8, 1); sum = num1(8, Ke, sum); Ke++; break;
		case 11:LCD_ShowNum(f(line), Ke, 9, 1); sum = num1(9, Ke, sum); Ke++; break;

		case 12: {
			if (Ke == 1)break;
			else {
				LCD_ShowChar(f(line), Ke, '*');
				mode = 3;
				Ke++;
				break;
			}
		}

		case 13: {
			if (Ke != 1) {
				Ke--;
				LCD_ShowChar(f(line), Ke, ' ');
				sum = back(sum, Ke);
				break;
			}
			else { sum = 0; break; }
		}

		case 14:LCD_ShowNum(f(line), Ke, 0, 1); sum = num1(0, Ke, sum); Ke++; break;
		case 15:LCD_Init(); Ke = 1; sum = 0; break;

		case 16: {
			if (Ke == 1)break;
			else {
				LCD_ShowChar(f(line), Ke, '/');
				mode = 4;
				Ke++;
				break;
			}
		}
		}
		if (mode != 0) {
			break;
		}
	}

	sum1 = sum;
	sum = 0;
	KKe = Ke;


	//------------------------num2-------------------



	while (2) {
		switch (Key()) {
		case 1:LCD_ShowNum(f(line), Ke, 1, 1); sum = num2(1, Ke, sum); Ke++; break;
		case 2:LCD_ShowNum(f(line), Ke, 2, 1); sum = num2(2, Ke, sum); Ke++; break;
		case 3:LCD_ShowNum(f(line), Ke, 3, 1); sum = num2(3, Ke, sum); Ke++; break;

		case 5:LCD_ShowNum(f(line), Ke, 4, 1); sum = num2(4, Ke, sum); Ke++; break;
		case 6:LCD_ShowNum(f(line), Ke, 5, 1); sum = num2(5, Ke, sum); Ke++; break;
		case 7:LCD_ShowNum(f(line), Ke, 6, 1); sum = num2(6, Ke, sum); Ke++; break;

		case 9:LCD_ShowNum(f(line), Ke, 7, 1); sum = num2(7, Ke, sum); Ke++; break;
		case 10:LCD_ShowNum(f(line), Ke, 8, 1); sum = num2(8, Ke, sum); Ke++; break;
		case 11:LCD_ShowNum(f(line), Ke, 9, 1); sum = num2(9, Ke, sum); Ke++; break;


		case 13: {
			if (Ke != KKe) {
				Ke--;
				LCD_ShowChar(f(line), Ke, ' ');
				sum = back(sum, Ke);
				break;
			}
			else { sum = 0; break; }
		}

		case 14:LCD_ShowNum(f(line), Ke, 0, 1); sum = num2(0, Ke, sum); Ke++; break;

		}

		if (P3_1 == 0)break;

	}
	//-----------------------------------show---------------------
lab1:

	Delay1ms(20);
	while (P3_1 == 0);
	Delay1ms(20);


	switch ((int)mode) {
	case 1: {
		if (sum1 + sum >= 0) {
			LCD_ShowNum(line, 1, sum1 + sum, weishu(sum1 + sum));
			sum1 = sum1 + sum; sum = 0; KKe = Ke = weishu(sum1 + sum) + 1;
			break;
		}
		else {
			LCD_ShowChar(line, 1, '-');
			LCD_ShowNum(line, 2, (-(sum + sum1)), weishu(-(sum + sum1)));
			KKe = Ke = weishu(-(sum + sum1)) + 2;
			sum1 = sum1 + sum; sum = 0;
		}
	}
	case 2: {
		if (sum1 - sum >= 0) {
			LCD_ShowNum(line, 1, sum1 - sum, weishu(sum1 - sum));
			KKe = Ke = weishu(sum1 - sum) + 1;
			break;
		}
		else {
			LCD_ShowChar(line, 1, '-');
			LCD_ShowNum(line, 2, sum - sum1, weishu(sum - sum1));
			KKe = Ke = weishu(sum - sum1) + 2;
		}
		sum1 = sum1 - sum; sum = 0;
		break;
	}

	case 3: {
		if (sum1 * sum >= 0) {
			LCD_ShowNum(line, 1, sum1 * sum, weishu(sum1 * sum));
			sum1 = sum1 * sum; sum = 0; KKe = Ke = weishu(sum1 * sum) + 1;
			break;
		}
		else {
			LCD_ShowChar(line, 1, '-');
			LCD_ShowNum(line, 2, (-(sum * sum1)), weishu(-(sum * sum1)));
			KKe = Ke = weishu(-(sum * sum1)) + 2;
			sum1 = sum * sum1; sum = 0;
			break;
		}
	}

	case 4: {
		if (sum == 0) {
			LCD_ShowString(line, 1, "Error!");
			while (1);
		}
		else {
			if (sum1 / sum >= 0) {

				LCD_ShowNum(line, 1, sum1 / sum, weishu(sum1 / sum));
				KKe = Ke = weishu(sum1 / sum) + 1;
				sum1 = sum1 / sum;
				sum = 0;
				break;

			}

			else {
				LCD_ShowChar(line, 1, '-');
				LCD_ShowNum(line, 2, (-(sum / sum1)), weishu(-(sum / sum1)));
				KKe = Ke = weishu(-(sum / sum1)) + 2;
				sum1 = sum / sum1; sum = 0;
				break;


			}


		}
	}
	}
	//------------------------------add show------------------------------------

	mode = 0;


	while (3) {
		switch (Key()) {

		case 4: {
			LCD_ShowChar(line, Ke, '+');
			mode = 1;
			Ke++;
			break;
		}

		case 8: {

			LCD_ShowChar(line, Ke, '-');
			mode = 2;
			Ke++;
			break;
		}

		case 12: {
			LCD_ShowChar(line, Ke, '*');
			mode = 3;
			Ke++;
			break;
		}


		case 16: {
			LCD_ShowChar(line, Ke, '/');
			mode = 4;
			Ke++;
			break;
		}

		}
		if (mode != 0) {
			LCD_ShowString(f(line), 1, "                                  ");
			break;
		}
	}

	KKe = Ke;
	//-----------------------------------------add number--------------------------
	while (4) {
		switch (Key()) {
		case 1:LCD_ShowNum(line, Ke, 1, 1); sum = num2(1, Ke, sum); Ke++; break;
		case 2:LCD_ShowNum(line, Ke, 2, 1); sum = num2(2, Ke, sum); Ke++; break;
		case 3:LCD_ShowNum(line, Ke, 3, 1); sum = num2(3, Ke, sum); Ke++; break;

		case 5:LCD_ShowNum(line, Ke, 4, 1); sum = num2(4, Ke, sum); Ke++; break;
		case 6:LCD_ShowNum(line, Ke, 5, 1); sum = num2(5, Ke, sum); Ke++; break;
		case 7:LCD_ShowNum(line, Ke, 6, 1); sum = num2(6, Ke, sum); Ke++; break;

		case 9:LCD_ShowNum(line, Ke, 7, 1); sum = num2(7, Ke, sum); Ke++; break;
		case 10:LCD_ShowNum(line, Ke, 8, 1); sum = num2(8, Ke, sum); Ke++; break;
		case 11:LCD_ShowNum(line, Ke, 9, 1); sum = num2(9, Ke, sum); Ke++; break;


		case 13: {
			if (Ke != KKe) {
				Ke--;
				LCD_ShowChar(line, Ke, ' ');
				sum = back(sum, Ke);
				break;
			}
			else { sum = 0; break; }
		}

		case 14:LCD_ShowNum(line, Ke, 0, 1); sum = num2(0, Ke, sum); Ke++; break;
		}
		if (P3_1 == 0)break;
	}

	//---------------------------------show 2----------------


//---------------------------------------again----------------------------------
	line = f(line);
	goto lab1;
}